﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class GoodsForm : Form
    {
        public GoodsForm()
        {
            InitializeComponent();
        }

        #region Method

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strConstrolName"></param>
        /// <returns></returns>
        private bool CheckProcess(string strConstrolName)
        {
            string strValue = string.Empty;
            try
            {
                switch (strConstrolName)
                {
                    case "txtBUYING_PRICE":
                        strValue = this.txtBUYING_PRICE.Text.Trim();
                        if (!Method.CheckIsNumber(strValue,8,true,true))
                        {
                            txtBUYING_PRICE.Focus();
                            return false;
                        }
                        break;
                    case "txtSELLING_PRICE":
                        strValue = this.txtSELLING_PRICE.Text.Trim();
                        if (!Method.CheckIsNumber(strValue, 8, true, true))
                        {
                            txtSELLING_PRICE.Focus();
                            return false;
                        }
                        break;
                    case "txtOVER_DATE":
                        strValue = this.txtOVER_DATE.Text.Trim();
                        if (!Method.CheckIsNumber(strValue, 8, true, true))
                        {
                            txtOVER_DATE.Focus();
                            return false;
                        }
                        break;
                    case "txtMAX_WARN":
                        strValue = this.txtMAX_WARN.Text.Trim();
                        if (!Method.CheckIsNumber(strValue, 8, true, true))
                        {
                            txtMAX_WARN.Focus();
                            return false;
                        }
                        break;
                    case "txtMIN_WARN":
                        strValue = this.txtMIN_WARN.Text.Trim();
                        if (!Method.CheckIsNumber(strValue, 8, true, true))
                        {
                            txtMIN_WARN.Focus();
                            return false;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteClientErrLog4Net(ex);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumStyle"></param>
        /// <returns></returns>
        protected bool CheckProcessLoop(enumOperation operation)
        {
            switch (operation)
            {
                case enumOperation.Add:
                    if (!CheckProcess("txtBUYING_PRICE")) return false;
                    if (!CheckProcess("txtSELLING_PRICE")) return false;
                    if (!CheckProcess("txtOVER_DATE")) return false;
                    if (!CheckProcess("txtMAX_WARN")) return false;
                    if (!CheckProcess("txtMIN_WARN")) return false;
                    break;
                default:
                    break;
            }
            return true;
        }

        #endregion

        #region Event

        /// <summary>
        /// ADD
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            if (!CheckProcessLoop(enumOperation.Add))
            {
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                if (Method.MessageShow("Q12002") == DialogResult.Yes)
                {
                    BLL.BLL_GOODS bll_Goods = new BLL_GOODS();
                    MODEL.GOODS goods = new MODEL.GOODS();
                    goods.TRANS_STATE = "1";
                    goods.UPDATER_ID = GlobalVal.gloUserInfo.USER_LOGIN_ID;
                    goods.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
                    goods.GOODS_ID = txtGOODS_ID.Text.Trim();
                    goods.GOODS_NO = txtGOODS_NO.Text.Trim();
                    goods.GOODS_NM1 = txtGOODS_NM1.Text.Trim();
                    goods.GOODS_NM2 = txtGOODS_NM2.Text.Trim();
                    goods.GOODS_GROUP = cmbGOODS_GROUP.Text.Trim();
                    goods.PROVIDER = txtPROVIDER.Text.Trim();
                    goods.PRODUCE_DATE = txtPRODUCE_DATE.Text.Trim();
                    goods.OVER_PRODUCE_DATE = txtOVER_PRODUCE_DATE.Text.Trim();
                    if (txtOVER_DATE.Text.Trim().Length == 0)
                    {
                        goods.OVER_DATE = -1;
                    }
                    else
                    {
                        goods.OVER_DATE = int.Parse(txtOVER_DATE.Text.Trim());
                    }
                    if (txtMAX_WARN.Text.Trim().Length == 0)
                    {
                        goods.MAX_WARN = -1;
                    }
                    else
                    {
                        goods.MAX_WARN = int.Parse(txtMAX_WARN.Text.Trim());
                    }
                    if (txtMIN_WARN.Text.Trim().Length == 0)
                    {
                        goods.MIN_WARN = -1;
                    }
                    else
                    {
                        goods.MIN_WARN = int.Parse(txtMIN_WARN.Text.Trim());
                    }
                    if (txtBUYING_PRICE.Text.Trim().Length == 0)
                    {
                        goods.BUYING_PRICE = -1;
                    }
                    else
                    {
                        goods.BUYING_PRICE = int.Parse(txtBUYING_PRICE.Text.Trim());
                    }
                    if (txtSELLING_PRICE.Text.Trim().Length == 0)
                    {
                        goods.SELLING_PRICE = -1;
                    }
                    else
                    {
                        goods.SELLING_PRICE = int.Parse(txtSELLING_PRICE.Text.Trim());
                    }
                    goods.MEMO = txtMEMO.Text.Trim();
                    string strReturn = bll_Goods.Add_GOODS(goods);
                    
                    if (strReturn.Trim() == "0")
                    {
                        Method.MessageShow("I11001");
                    }
                    else
                    {
                        Method.MessageShow(strReturn);
                    }
                }
                this.Cursor = Cursors.Default;
            }
        }

        private void GoodsForm_Load(object sender, EventArgs e)
        {
            
        }

        #endregion
    }
}